// pages/article/article.js
const config = require('../../config.js');
const app = getApp();
Page({

    /**
     * 页面的初始数据
     */
    data: {
        isLoading: true, // 判断是否尚在加载中
        article: {},
        articleData: {},
        fullLoading: true,
        showCopyLink: false,
        link: "",
        isOpenComment: false,
        isNoComment: false,
        commentLoding: true,
        commentData: [],
        total: 0,
        pageIndex: 1,
        id: '',
        showCommentForm: false,
        commentFormTitle: "发表评论",
        pid: '',
        topPid: '',
        commentContent: ''
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        let that = this;
        that.getSetting();
        that.setData({
            id: options.id
        });
        wx.request({
            url: config.basePath + '/mini/article/getArticleById',
            method: 'GET',
            data: {
                articleId: options.id
            },
            success(res) {
                if (res.data.code === 200) {
                    wx.setNavigationBarTitle({
                        title: res.data.data.title
                    })
                    
                    that.setData({
                        articleData: res.data.data
                    });
                    let result = app.towxml(that.data.articleData.content, that.data.articleData.contentModel, {
                        base: config.basePath, // 相对资源的base路径
                        theme: 'light', // 主题，默认`light`
                        events: { // 为元素绑定的事件方法
                            tap: (e) => {
                                console.log(e);
                                if (e.currentTarget.dataset.data && e.currentTarget.dataset.data.attrs && (e.currentTarget.dataset.data.tag == "img")) {
                                    wx.previewImage({
                                        current: e.currentTarget.dataset.data.attrs.src,
                                        urls: [e.currentTarget.dataset.data.attrs.src]
                                    })
                                }

                                if (e.currentTarget.dataset.data && e.currentTarget.dataset.data.attrs && (e.currentTarget.dataset.data.tag == "navigator")) {
                                    that.setData({
                                        link: e.currentTarget.dataset.data.attrs.href,
                                        showCopyLink: true
                                    });
                                }
                            }
                        }
                    });

                    that.setData({
                        article: result,
                        isLoading: false,
                        fullLoading: false
                    });
                } else {
                    wx.lin.showMessage({
                        type: 'error',
                        content: res.data.msg
                    })
                }
            },
            fail(err) {
                wx.lin.showMessage({
                    type: 'error',
                    content: '网络出错'
                })
            }
        });

        that.getCommentList();
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {
        var that = this;
        if (that.data.total <= that.data.commentData.length) {
            return;
        }
        that.setData({
            pageIndex: that.data.pageIndex + 1
        });
        that.getCommentList();
    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    },
    onShareTimeline(res) {
        let that = this;
        return {
            title: that.data.articleData.title,
            path: '/pages/article/article?id=' + that.data.articleData.id,
            imageUrl: (that.data.articleData.thumbnail == null || that.data.articleData.thumbnail == "") ? "" :  that.data.articleData.thumbnail
        }
    },

    copyLink: function () {
        let that = this;
        wx.setClipboardData({
            data: that.data.link,
            success: function (res) {}
        })
    },

    getCommentList: function () {
        let that = this;
        that.setData({
            commentLoding: true
        });
        wx.request({
            url: config.basePath + '/mini/comment/getCommentByArticleId',
            method: 'GET',
            data: {
                pageIndex: that.data.pageIndex,
                pageSize: 3,
                articleId: that.data.id
            },
            success(res) {
                that.setData({
                    isNoComment: res.data.total <= 0,
                    commentData: that.data.commentData.concat(res.data.data),
                    total: res.data.total,
                    commentLoding: true
                });

                if (res.data.total <= that.data.commentData.length) {
                    that.setData({
                        commentLoding: false
                    });
                }
            },
            fail(err) {
                that.setData({
                    commentLoding: false
                });
                wx.lin.showMessage({
                    type: 'error',
                    content: '网络出错'
                })
            }
        });
    },
    formSubmit: function (e) {
        let that = this;
        wx.request({
            url: config.basePath + '/mini/comment/submitComment',
            method: 'POST',
            header: {
                "Content-Type": "application/x-www-form-urlencoded"
            },
            data: {
                articleId: that.data.id,
                content: e.detail.value.content,
                userName: e.detail.value.userName,
                email: e.detail.value.email,
                pid: that.data.pid,
                topPid: that.data.topPid
            },
            success(res) {
                if (res.data.code != 200) {
                    wx.lin.showMessage({
                        type: 'error',
                        content: res.data.msg
                    })
                    return;
                } else {
                    wx.lin.showMessage({
                        type: 'success',
                        content: "发表成功"
                    });
                    that.setData({
                        showCommentForm: false,
                        pageIndex: 1,
                        commentData: [],
                        total: 0,
                        pid: '',
                        topPid: '',
                        commentContent: ''
                    });
                    that.getCommentList();
                }
            },
            fail(err) {
                wx.lin.showMessage({
                    type: 'error',
                    content: '网络出错'
                })
            }
        });
    },
    formReset: function () {
        this.setData({
            showCommentForm: false,
            pid: '',
            topPid: '',
            commentContent: ''
        });
    },
    showCommentForm: function () {
        this.setData({
            commentFormTitle: "发表评论",
            showCommentForm: true
        });
    },
    reply: function (event) {
        console.log(event.currentTarget.dataset);
        this.setData({
            commentFormTitle: "回复" + event.currentTarget.dataset.name,
            pid: event.currentTarget.dataset.pid,
            topPid: event.currentTarget.dataset.toppid,
            showCommentForm: true
        });
    },
    getSetting: function () {
        let that = this;
        wx.request({
            url: config.basePath + '/mini/system/getSetting',
            method: 'GET',
            success(res) {
                if (res.data.code === 200) {
                    if (res.data.data.isOpenComment == "1") {
                        that.setData({
                            isOpenComment: true
                        });
                    }
                } else {
                    wx.lin.showMessage({
                        type: 'error',
                        content: res.data.msg
                    })
                }
            },
            fail(err) {
                wx.lin.showMessage({
                    type: 'error',
                    content: '网络出错'
                })
            }
        });
    }
})